package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/packing")
@Slf4j
public class PackingController extends BaseController{
    @Reference
    private PackingService packingService;
    @Reference
    private ExportService exportService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize){
        PageInfo pageInfo = packingService.findByPage(pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "/cargo/packing/packing-list";
    }




    @RequestMapping("/save")
    public String save(Packing packing){
        packingService.save(packing);

        return "redirect:/cargo/packing/list";
    }


    @RequestMapping("/delete")
    public String delete(String id){
        Packing packing = packingService.findById(id);
        if (packing.getState()==0){
            packingService.delete(id);
        }
        return "redirect:/cargo/packing/list";
    }

    @RequestMapping("/toView")
    public String toView(String id){
        Packing packing = packingService.findById(id);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-view";
    }

    @RequestMapping("/toShipping")
    public String toShipping(String id){
        Packing packing = packingService.findById(id);

        if(packing.getState()==1){
            return "redirect:/cargo/packing/list";
        }

        //获取装箱单的对应的合同的id
        String ids = packing.getExportIds();
        String[] idArrays = ids.split(",");
        Export export = exportService.findById(idArrays[0]);
        ShippingOrder shippingOrder = new ShippingOrder();
        shippingOrder.setShippingOrderId(id);
        shippingOrder.setLcNo(export.getLcno());
        shippingOrder.setPortOfLoading(export.getShipmentPort());
        shippingOrder.setPortOfDischar(export.getDestinationPort());
        shippingOrder.setNotifyParty(export.getConsignee());
        String marks = export.getMarks();

        request.setAttribute("id",id);
        request.setAttribute("marks",marks);
        request.setAttribute("shippingOrder",shippingOrder);
        return "cargo/packing/packing-toShipping";
    }
}
